// pages/clockin/clockin.js
const api = require('../../util')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: app.globalData.statusBarHeight,

        spot: [], // 下标数组
        wtnowy: '',
        wtnowm: '',
        wtnowd: '',
        dateString: "", // 当前选择日期
        olddateString: '', // 拷贝的当前日期
        switchtype: false, // 当前日期是否可以打卡
        list: [],
        checkedates: '',
        dkdetails: [],
    },
    // 获取打卡日期
    CheckedDate() {
        var that = this
        api.http('/MedicationReminder/CheckedDate', 'POST', false, {
            date: this.data.checkedates
        }, function (res) {
            if(res.code == 200) {
                that.setData({
                    spot: res.data
                })
            }
        })
    },
    // 打卡信息
    CheckedDetails(e) {
        var that = this
        api.http('/MedicationReminder/CheckedDetails', 'POST', false, {
            date: e
        }, function (res) {
            if(res.code == 200) {
                that.setData({
                    dkdetails: res.data
                })
            }
        })
    },
    // 打卡记录
    toclockinrecords() {
        wx.navigateTo({
          url: '/pages/clockinrecords/clockinrecords',
        })
    },
    // 日历切换
    dateChange(e) {
        var that = this
        console.log("选中日期变了,现在日期是", e.detail.dateString)
        var type = true
        if (this.data.olddateString >= e.detail.dateString) {
            type = false
        }
        this.setData({
          dateString: e.detail.dateString,
          switchtype: type
        }, () => {
            var str = e.detail.dateString.substring(0,7)
            if (that.data.checkedates!=str) {
                this.setData({
                    checkedates: str
                }, () => {
                    that.CheckedDate()
                })
            }
            that.CheckedDetails(e.detail.dateString)
        })
    },
    // 打卡
    switch1Change(e) {
        var status = 0
        if (e.currentTarget.dataset.status === 0) {
            status = 1
        }
        var that = this
        api.http('/MedicationReminder/clockIn', 'POST', true, {
            status,
            id: e.currentTarget.dataset.id
        }, function (res) {
            if(res.code == 200) {
                var indexs = e.currentTarget.dataset.indexs,index=e.currentTarget.dataset.index
                var str = 'dkdetails['+index+'].list['+indexs+'].status'
                that.setData({
                    [str]: status
                })
                wx.showToast({
                    title: res.msg,
                    icon: 'success',
                    duration: 1000
                })
                setTimeout(() => {
                    that.CheckedDate()
                    // that.CheckedDetails(that.data.dateString)
                }, 500);
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1000
                })
                var indexs = e.currentTarget.dataset.indexs,index=e.currentTarget.dataset.index
                var str = 'dkdetails['+index+'].list['+indexs+'].status'
                that.setData({
                    [str]: e.currentTarget.dataset.status
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if(wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法滚动回顶部，请升级到最新微信版本后重试。'
            })
        }
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                select: 1
            })
        }
        var that = this
        let myDate = new Date()
        let myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
        let myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
        let myToday = myDate.getDate(); //获取当前日(1-31)
        myMonth = myMonth > 9 ? myMonth : '0' + myMonth
        myToday = myToday > 9 ? myToday : '0' + myToday
        let nowDate = myYear + '-' + myMonth + '-' + myToday
        this.setData({
            dateString: nowDate,
            olddateString: JSON.parse(JSON.stringify(nowDate)),
            wtnowy: myYear,
            wtnowm: myMonth,
            wtnowd: myToday,
            checkedates: myYear+'-'+myMonth
        }, () => {})
        if (wx.getStorageSync(app.globalData.miniProgramtype+'access_token')=='') {
            app.wxlogin().then(()=>{
                that.CheckedDate()
                that.CheckedDetails(that.data.dateString)
            })
        } else {
            that.CheckedDate()
            that.CheckedDetails(that.data.dateString)
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage() {

    // }
})